from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from apps.goods.models import SKUImage, SKU
from apps.meiduo_admin.serializers.image import ImageSeriazlier, SKUSeriazlier
from apps.meiduo_admin.utils import PageNum


class ImageView(ModelViewSet):
    # 图片序列化器
    queryset = SKUImage.objects.all()
    # 图片查询集
    serializer_class = ImageSeriazlier
    # 分页
    pagination_class = PageNum

    # 新增
    def create(self, request, *args, **kwargs):
        # 接收参数 校验
        # 获取sku_id
        sku_id = request.data.get('sku')
        # 获取前端传递的image文件
        image = request.FILES.get('image')
        # print('3333', image)
        from fdfs_client.client import Fdfs_client
        # 创建FastDFS连接对象
        client = Fdfs_client('utils/fastdfs/client.conf')
        # print('11111', client)
        # 上传
        # result = client.upload_appender_by_buffer(image.read())
        # result=client.upload_appender_by_buffer(image.read())
        result = client.upload_by_buffer(image.read())
        # print('2222', result)
        if result.get('Status') != 'Upload successed.':
            return Response(status=status.HTTP_400_BAD_REQUEST)

        file_id = result.get('Remote file_id')

        SKUImage.objects.create(sku_id=sku_id, image=file_id)

        return Response(status=status.HTTP_201_CREATED)

    # 修改
    def update(self, request, *args, **kwargs):
        # 接收参数
        sku_id = request.data.get('sku')
        image = request.FILES.get('image')
        # 获取要修改的模型类对象id
        pk = kwargs.get('pk')
        # print('kwargs', kwargs)

        # 把图片上传到fastdfs里 返回一个图片地址
        from fdfs_client.client import Fdfs_client
        # 创建FastDFS链接对象
        client = Fdfs_client('utils/fastdfs/client.conf')
        # 上传
        result = client.upload_by_buffer(image.read())
        if result.get('Status') != 'Upload successed.':
            return Response(status=status.HTTP_400_BAD_REQUEST)

        # 取出fastdfs里的地址 把这个地址 保存到数据库里
        file_id = result.get('Remote file_id')
        print('file_id', file_id)
        # 获取要修改的对象
        sku_image = SKUImage.objects.get(id=pk)
        # 把图片的地址补充上
        sku_image.image = file_id
        # 保存数据
        sku_image.save()
        # 返回响应
        return Response(status=status.HTTP_201_CREATED)

    # 删除
    def destroy(self, request, *args, **kwargs):
        # 获取要删除对象的id
        pk = kwargs.get('pk')
        # print('kwargs', kwargs)
        # print('pk', pk)
        # 获取要删除对象
        sku_image = SKUImage.objects.get(id=pk)
        # 图片地址
        image = sku_image.image.url
        # print('image', image)
        # 把图片从fastdfs里删除
        from fdfs_client.client import Fdfs_client
        # 创建FastDFS链接对象
        client = Fdfs_client('utils/fastdfs/client.conf')
        # print('client', client)
        # url = 'http://192.168.92.128:8888/'
        image = image.split('http://192.168.92.128:8888/')
        # print('image1111', image[1])
        # print(type(image))
        client.delete_file(image[1])
        sku_image.delete()
        return Response(status=status.HTTP_201_CREATED)


class SKUView(APIView):

    def get(self, request):
        data = SKU.objects.all()
        ser = SKUSeriazlier(data, many=True)
        return Response(ser.data)
